package com.easylinkin.linkappapi.meterbilling.controller;

import cn.hutool.core.util.StrUtil;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.meterbilling.entity.MeterRemedy;
import com.easylinkin.linkappapi.meterbilling.entity.ext.MeterRemedyExt;
import com.easylinkin.linkappapi.meterbilling.service.MeterRemedyService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * class info :抄表补录控制器
 *
 * @author liuqihang
 * @date 2021/8/3 15:21
 */
@RestController
@RequestMapping("/remedy")
public class MeterRemedyController {

    @Resource
    private MeterRemedyService meterRemedyService;

    @PostMapping("page")
    public RestMessage page(@RequestBody RequestModel<MeterRemedyExt> requestModel){
        Assert.notNull(requestModel, "参数为空");
        Assert.notNull(requestModel.getPage(), "参数page为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams参数为空");
        return  RestBuilders.successBuilder().data(meterRemedyService.getPage(requestModel.getPage(),requestModel.getCustomQueryParams())).build();
    }

    @PostMapping("saveRecord")
    public RestMessage saveRecord(@RequestBody MeterRemedy meterRemedy) throws Exception {
        Assert.notNull(meterRemedy, "参数为空");
        Assert.notNull(meterRemedy.getRoomId(), "房间ID参数为空");
        Assert.notNull(meterRemedy.getDeviceCode(), "设备编码参数为空");
        Assert.notNull(meterRemedy.getEnergyAmount(), "设备读数参数为空");
        Assert.notNull(meterRemedy.getRemark(), "备注参数为空");
        Assert.notNull(meterRemedy.getRemedyTime(), "补录时间参数为空");
        Assert.notNull(meterRemedy.getType(), "设备类型不能为空");
        String msg = meterRemedyService.addRemedyRecord(meterRemedy);
        if(StrUtil.isEmpty(msg)){
            return  RestBuilders.successBuilder().build();
        }else {
            return  RestBuilders.successBuilder().message(msg).build();
        }

    }

    @PostMapping("export")
    public void export(@RequestBody MeterRemedyExt meterRemedyExt, HttpServletRequest request, HttpServletResponse response) {
        meterRemedyService.export(meterRemedyExt, request, response);
    }
}
